package com.web;

import com.DisplayChatters;
import com.NicknameValidation;
import com.dao.ChatterDAO;
import com.domain.Chatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/start")
public class StartController {
    @Resource
    private NicknameValidation nicknameValidation;

    @Resource
    private DisplayChatters displayChatters;

    @Resource
    private ChatterDAO chatterDAO;


    @RequestMapping("/start")
    public String start(@RequestParam(value = "login", required = false) String login, HttpSession session) {
        if (nicknameValidation.validate(login)) {
            if (login != null) {
                Chatter chatter = new Chatter();
                chatter.setName(login);
                chatterDAO.addUser(chatter);
                session.setAttribute("nickname", login);
                return "chat";
            }
        }

        session.setAttribute("users", displayChatters.getUsers());

        return "start";
    }
}
